package com.ardf.controller;

import com.ardf.entity.Athlete;
import com.ardf.entity.Unit;
import com.ardf.entity.dto.AthleteHasOneDto;
import com.ardf.entity.dto.AthleteTableDto;
import com.ardf.entity.vo.AddOneAthleteVo;
import com.ardf.entity.vo.EditAthleteVo;
import com.ardf.service.IAthleteService;
import com.ardf.service.IUnitService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ShiYunHao
 * @since 2025-01-06
 */
@RestController
@RequestMapping("/ardf/athlete")
public class AthleteController {
    private final IAthleteService athleteService;
    private final IUnitService unitService;

    public AthleteController(IAthleteService athleteService, IUnitService unitService) {
        this.athleteService = athleteService;
        this.unitService = unitService;
    }


    /**
     * 模板下载
     */
    @GetMapping("download")
    public void download(HttpServletResponse response) throws IOException {
        athleteService.download(response);
    }

    /**
     * 导入选手
     */
    @PostMapping("/upload")
    public void upload(@RequestParam("file") MultipartFile file,
                       @RequestParam("competitionId") Long competitionId,
                       HttpServletResponse response) throws IOException {
        athleteService.upload(file, competitionId, response);
    }

    /**
     * 检查是否有选手
     */
    @GetMapping("/hasOne")
    public ResponseEntity<AthleteHasOneDto> hasOne(@RequestParam("competitionId") Long competitionId) {
        List<Long> unitIds = unitService.listObjs(new LambdaQueryWrapper<Unit>().select(Unit::getId)
                .eq(Unit::getCompetitionId, competitionId));
        AthleteHasOneDto athleteHasOneDto = new AthleteHasOneDto();
        for (Long unitId : unitIds) {
            long count = athleteService.count(new LambdaQueryWrapper<Athlete>()
                    .eq(Athlete::getUnitId, unitId));
            if (count > 0) {
                athleteHasOneDto.setHasOne(true);
                return ResponseEntity.ok(athleteHasOneDto);
            }
        }
        athleteHasOneDto.setHasOne(false);
        return ResponseEntity.ok(athleteHasOneDto);
    }

    /**
     * 分页
     *
     * @param pageSize 每页多少条
     * @param pageNum  第几页
     * @return 分页数据
     */
    @GetMapping("/page")
    public ResponseEntity<Page<AthleteTableDto>> page(@RequestParam("pageSize") Integer pageSize,
                                                      @RequestParam("pageNum") Integer pageNum,
                                                      @RequestParam("competitionId") Long competitionId) {
        Page<AthleteTableDto> page = athleteService.getAthleteTablePage(pageSize, pageNum, competitionId);
        return ResponseEntity.ok(page);
    }

    /**
     * 搜索查询
     */
    @GetMapping("/searchAll")
    public ResponseEntity<Page<AthleteTableDto>> searchAll(@RequestParam("pageSize") Integer pageSize,
                                                           @RequestParam("pageNum") Integer pageNum,
                                                           @RequestParam("competitionId") Long competitionId,
                                                           @RequestParam(value = "searchAll", required = false) String searchAll) {
        Page<AthleteTableDto> page;
        if (searchAll!=null&&StringUtils.hasLength(searchAll.trim())) {
            page = athleteService.searchAll(pageSize, pageNum, competitionId, searchAll.trim());
        } else {
            page = athleteService.searchAll(pageSize, pageNum, competitionId, null);
        }
        return ResponseEntity.ok(page);
    }

    /**
     * 单个删除
     */
    @PostMapping("/deleteById")
    public void deleteById(@RequestParam("id") Long id) {
        athleteService.deleteById(id);
    }

    /**
     * 批量删除
     */
    @PostMapping("/deleteBatch")
    public void deleteBatch(@RequestBody List<Long> ids) {
        athleteService.deleteAll(ids);
    }

    /**
     * 单个增加
     */
    @PostMapping("/addOne")
    public void addOne(@RequestBody AddOneAthleteVo addOneAthleteVo) {
        athleteService.addOne(addOneAthleteVo);
    }

    /**
     * 检查身份证号是否重复
     */
    @GetMapping("/check")
    public ResponseEntity<String> check(@RequestParam("identity") String identity,
                                        @RequestParam("competitionId") Long competitionId) {
        List<Long> unitIds = unitService.listObjs(new LambdaQueryWrapper<Unit>().select(Unit::getId)
                .eq(Unit::getCompetitionId, competitionId));
        if (unitIds != null && !unitIds.isEmpty()) {
            List<Athlete> list = athleteService.list(new LambdaQueryWrapper<Athlete>()
                    .eq(Athlete::getIdentity, identity)
                    .in(Athlete::getUnitId, unitIds));
            if (list == null || list.isEmpty()) {
                return ResponseEntity.ok("yes");
            } else {
                return ResponseEntity.ok("no");
            }
        } else {
            return ResponseEntity.ok("yes");
        }
    }

    /**
     * 根据身份证号查找选手
     */
    @GetMapping("/getByIdNum")
    public ResponseEntity<Athlete> getByIdNum(@RequestParam("identity") String identity,
                                              @RequestParam("competitionId") Long competitionId) {
        List<Long> unitIds = unitService.listObjs(new LambdaQueryWrapper<Unit>().select(Unit::getId)
                .eq(Unit::getCompetitionId, competitionId));
        if (unitIds != null && !unitIds.isEmpty()) {
            List<Athlete> list = athleteService.list(new LambdaQueryWrapper<Athlete>()
                    .eq(Athlete::getIdentity, identity)
                    .in(Athlete::getUnitId, unitIds));
            if (list == null || list.isEmpty()) {
                return ResponseEntity.ok(new Athlete());
            } else {
                return ResponseEntity.ok(list.get(0));
            }
        } else {
            return ResponseEntity.ok(new Athlete());
        }
    }
    /**
     * 编辑选手个人信息
     */
    @PostMapping("/editOne")
    public void editOne(@RequestBody EditAthleteVo editAthleteVo){
        athleteService.editOne(editAthleteVo);
    }
}
